-- 运营活动: 聚宝盆


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 53, -- 活动id (53 聚宝盆)
    act_name = "聚宝盆", -- 活动名称
    act_desc = "活动期间，玩家只要达到对应的充值金额，即可投入对应的元宝，赚取随机数额的元宝", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1593267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        maxCount = 10,  --最大次数
        saveCount = 50, --保存最多信息
        awrads = 
        {
            {       --第一次
                needRechargeGoldNum = 100,  --需要充值元宝
                costGoldNum = 1000,     --需要消耗元宝
                award =             --奖励
                {
                    {
                        weight = 100,       --权重
                        getGoldRange = {100, 10000},   --奖励范围
                    },
                    {
                        weight = 200,       --权重
                        getGoldRange = {10001, 20000},   --奖励范围
                    },
                },
            },
            {       --第二次
                needRechargeGoldNum = 100,  --需要充值元宝
                costGoldNum = 1000,     --需要消耗元宝
                award =             --奖励
                {
                    {
                        weight = 100,       --权重
                        getGoldRange = {100, 10000},   --奖励范围
                    },
                },
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_53_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end 

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_53_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_53_unsetup(cfg)   
end

-- 活动开始
function oper_act_53_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_53_start = 1
    ssVar.oper_act_53_cmd = cfg.cmd_id
    ssVar.oper_act_53_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_53, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_53)
    clearOperationWorldData(cfg.act_id)

end

-- 活动结束
function oper_act_53_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_53_start = nil
    ssVar.oper_act_53_cmd = nil
    ssVar.oper_act_53_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end
end

-- 打包数据
function oper_act_53_packet_data(sysarg, cfg)
    if not cfg then return end
    local config = cfg.config
    if not config then return end
    
    
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_53, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myUseCount then
        myData.myUseCount = 0
    end

    if not myData.myRechargeMoney then
        myData.myRechargeMoney = 0
    end
    
    local npack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (npack == nil) then
        return
    end

    DataPack.writeInt(npack, myData.myRechargeMoney)
    DataPack.writeByte(npack, myData.myUseCount)

    local dVar = Actor.getDyanmicVar(sysarg)
    local circle = dVar.oper_act_53_circle or 1
    local index = dVar.oper_act_53_index or 1

    local dataTable, curCicle, curIndex = getOperationWorldData(cfg.act_id, circle, index, config.saveCount)
    dVar.oper_act_53_circle = curCicle
    dVar.oper_act_53_index = curIndex
    if dataTable then
        local len = #dataTable
         --print("len ="..len)
        DataPack.writeByte(npack, len)
        for idx = 1, len do
            DataPack.writeString(npack, dataTable[idx].name)
            DataPack.writeInt(npack, dataTable[idx].id)  --特殊处理，id为消耗的元宝
            DataPack.writeInt(npack, dataTable[idx].count)  --特殊处理，count为获得的元宝      
        end
    else
        --print("11111")
        DataPack.writeByte(npack, 0)
    end

    DataPack.flush(npack) 
end

-- 操作，返回操作结果，0为成功
function oper_act_53_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_53_do cfg.config confg error")
        return
    end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_53, 1)

    local myData = BaseFuc_unserialize(data)
    
    if not myData then
        myData = {}
    end

    if not myData.myUseCount then
        myData.myUseCount = 0
    end

    if not myData.myRechargeMoney then
        myData.myRechargeMoney = 0
    end

    local cfg2 = config.awrads[myData.myUseCount + 1]
    if not cfg2 then
        return
    end

    if myData.myRechargeMoney < cfg2.needRechargeGoldNum then
        Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.Operation020, cfg2.needRechargeGoldNum), ttFlyTip )
        return
    end

    if Actor.getMoneyCount(sysarg, mtYuanbao) < cfg2.costGoldNum then
        Actor.sendItemLack( sysarg, qatYuanbao, 0, cfg2.costGoldNum )
    end

    local ii, myAwards = GetItemIdxRandExt(cfg2.award)
    if not myAwards then
        return
    end
    local awardsRange = myAwards.getGoldRange 
    if not awardsRange then
        return
    end

    local myGetGoldNum = System.getRandomNumber(awardsRange[1], awardsRange[2])
    if myGetGoldNum <= 0 then
        return
    end

    if not Actor.delMoney( sysarg,mtYuanbao,cfg2.costGoldNum,LogIds.OperationLogId053,Lang.LogTips.Operation053) then
        return
    end

    if Actor.addMoney( sysarg,mtYuanbao,myGetGoldNum,LogIds.OperationLogId053,Lang.LogTips.Operation053) then
        myData.myUseCount = myData.myUseCount + 1

        local time = System.getSystemTime()
        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        local newData = BaseFuc_serialize(myData)
        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_53, 1, newData)

        local saveData = {}
        saveData.name = Actor.getName(sysarg)
        saveData.type = 0
        saveData.id = cfg2.costGoldNum       --特殊处理，id为消耗的元宝
        saveData.count = myGetGoldNum       --特殊处理，count为获得的元宝
        setOperationWorldData(cfg.act_id, saveData, config.saveCount)

        oper_act_53_packet_data(sysarg, cfg)
        local msg = string.format(Lang.ScriptTips.Operation253, Actor.getName(sysarg), cfg2.costGoldNum, myGetGoldNum)
        System.broadcastTipmsg(msg,ttChatWindow + ttScreenCenter)
    end 
        
end

--[[
-- 跨天
function oper_act_53_new_day_arrive(act_id) 
    
end
]]


function oper_act_53_withdrawyuanbao(sysarg, nYuanBao)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_53_start then
        return
    end
    if not ssVar.oper_act_53_cmd then
        return
    end
    if not ssVar.oper_act_53_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_53_cmd, ssVar.oper_act_53_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_53, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    if not myData.myUseCount then
        myData.myUseCount = 0
    end

    if not myData.myRechargeMoney then
        myData.myRechargeMoney = 0
    end

    myData.myRechargeMoney = myData.myRechargeMoney + nYuanBao

    local time = System.getSystemTime()
    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_53, 1, newData)

    oper_act_53_packet_data(sysarg, cfg)
           
end